<template>
  <div>
    <div class="main">
      <div class="TopTitle">
        <h1>Annual Rank</h1>
        <form class="d-flex" role="search">
          <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search" v-model="inputVal" />
          <button class="btn btn-outline-success" type="submit">Search</button>
        </form>
      </div>
      <div class="products">
        <div class="part" v-for="(item,index) in searchList" :key="index" @click="goDetails(item,index)">
          <div class="rank">
            <h1>{{item.id}}</h1>
          </div>
          <div class="pic">
            <img :src="item.pic" alt />
          </div>
          <div class="text">
            <h2>{{item.name}}</h2>
            <h5>{{item.price}}</h5>
          </div>
          <div class="saleNumber">
            <h3>销售量</h3>
            <div class="badge bg-danger text-wrap" style="width: 6rem; font-size: 18px;">{{item.saleNumber}}台</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      inputVal:'',
      products: []
    };
  },
  mounted() {
    axios
      .get("https://mock.apifox.cn/m1/1255964-0-default/rank")
      .then(res => (console.log(res.data), (this.products = res.data)))
      .catch(function(error) {
        // 请求失败处理
        console.log(error);
      });
  },
  methods: {
    goDetails(item,index) {
      this.$store.state.detailId = index;
      this.$router.push({
        path: "/details",
        query: { name: "rank" ,id:item.id}
      });
    }
  },
     computed:{
     searchList () {
      if (!this.inputVal) {
        return this.products
      }
      const data = []
      const reg = new RegExp(this.inputVal.toLowerCase())
      this.products.forEach(item => {
        if (reg.test(item.name.toLowerCase())) {
          data.push(item)
        }
      })
      return data
    }
  }
};
</script>
<style lang="scss" scoped>
.main {
  width: 96%;
  margin: auto;
  margin: 20px;
  .TopTitle {
    height: 60px;
    width: 60%;
    margin-left: 20px;
    display: flex;
    justify-content: space-between;
    border-bottom: 2px black solid;
    box-shadow: 10px 5px 5px black;
    color: #000000;
    input {
      height: 45px;
    }
    button {
      height: 45px;
      margin-right: 20px;
    }
  }
  .products {
    width: 100%;
    margin-top: 20px;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    .part {
      width: 88%;
      height: 220px;
      margin: 12px 22px;
      border: #c5c5c5 2px solid;
      color: #000000;
      display: flex;
      flex-direction: row;
      .rank {
        width: 5%;
        height: 50px;
        margin: 80px 20px;
        border-radius: 50%;
        border: #c5c5c5 2px solid;
      }
      .pic {
        width: 16%;
        height: 160px;
        border: #c5c5c5 2px solid;
        margin-top: 40px;
        border-radius: 20px;
        img {
          width: 100%;
          height: 160px;
          border-radius: 10px;
        }
      }
      .text {
        width: 60%;
        height: 80px;
        border: #c5c5c5 2px solid;
        border-radius: 10px;
        margin: 60px;
      }
      .saleNumber {
        width: 16%;
        height: 80px;
        margin: 60px 20px;
        
      }
    }
    .part:hover {
      box-shadow: 10px 5px 5px rgb(45, 45, 45);
    }
  }
}
</style>